package xyz.xpman.prisonerfitness.entity;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;

import xyz.xpman.prisonerfitness.config.Constant;
import xyz.xpman.prisonerfitness.util.JsonUtil;

@SuppressWarnings("unused")
public class Chapter implements Serializable {
    public static final String ID = "id";
    public static final String TITLE = "title";
    public static final String DESCRIPTION = "description";
    public static final String GUIDE_IMG = "img";

    private String id;
    private String title = null;
    private String description = null;
    private Integer guideImgId = null;
    private String[] guideImg = new String[0];



    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getGuideImgId() {
        return guideImgId;
    }

    private void setGuideImgId(int guideImgId) {
        this.guideImgId = guideImgId;
    }

    public String[] getGuideImg() {
        return guideImg;
    }

    private void setGuideImg(@NotNull String[] guideImg) {
        if (null != guideImg) {
            this.guideImg = guideImg.clone();
        }
    }


    public boolean hasGuidImg() {
        return 0 != guideImg.length;
    }



    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



    public static Chapter byJson(@NotNull JSONObject json) {
        Chapter chapter = new Chapter();

        try {
            JSONObject item = json.getJSONObject(Constant.GUIDE_KEY);

            chapter.setId(json.getString(ID));
            chapter.setTitle(item.getString(TITLE));
            chapter.setDescription(item.getString(DESCRIPTION));


            JSONArray guideImgArray = item.getJSONArray(GUIDE_IMG);
            chapter.setGuideImg(JsonUtil.toStringArray(guideImgArray));

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return chapter;
    }

}
